/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simpleciphertext;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

/**
 *
 * @author user
 */
public class SimpleCipherTextClass {

    private static int count = 20;
    private static String transformation = "PBEWithMD5AndDES";
    private static byte[] salt = {
        (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
        (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
    };

    /**
     * Cifra o descifra un array de bytes en base a la clave establecida
     * @return array de bytes recodificado
     */
    public static byte[] recode(byte[] txt, String pass, int mode) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        PBEKeySpec pbeKeySpec;
        PBEParameterSpec pbeParamSpec;
        SecretKeyFactory keyFac;

        pbeParamSpec = new PBEParameterSpec(salt, count);

        pbeKeySpec = new PBEKeySpec(pass.toCharArray());
        keyFac = SecretKeyFactory.getInstance(transformation);
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        Cipher pbeCipher = Cipher.getInstance(transformation);

        pbeCipher.init(mode, pbeKey, pbeParamSpec);
        return pbeCipher.doFinal(txt);
    }

    /**
     * Cifra o descifra un array de bytes en base a la clave establecida
     * @return array de bytes recodificado
     */
    public static String recodeString(String txt, String pass, int mode) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        return new String(recode(txt.getBytes(), pass, mode));
    }

    public static byte []encrypt(byte []txt, String pass) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        return recode(txt, pass, Cipher.ENCRYPT_MODE);
    }
    
    public static String encryptString(String txt, String pass) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        return recodeString(txt, pass, Cipher.ENCRYPT_MODE);
    }
    
    public static byte []decrypt(byte []txt, String pass) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        return recode(txt, pass, Cipher.DECRYPT_MODE);
    }
    
    public static String decryptString(String txt, String pass) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException {
        return recodeString(txt, pass, Cipher.DECRYPT_MODE);
    }

    /**
     * @return the count
     */
    public static int getCount() {
        return count;
    }

    /**
     * @param aCount the count to set
     */
    public static void setCount(int aCount) {
        count = aCount;
    }

    /**
     * @return the transformation
     */
    public static String getTransformation() {
        return transformation;
    }

    /**
     * @param aTransformation the transformation to set
     */
    public static void setTransformation(String aTransformation) {
        transformation = aTransformation;
    }

    /**
     * @return the salt
     */
    public static byte[] getSalt() {
        return salt;
    }

    /**
     * @param aSalt the salt to set
     */
    public static void setSalt(byte[] aSalt) {
        salt = aSalt;
    }
}

